
package com.hyst.satelite;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hyst.config.Config;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * @Description:
 * @Date：2018年12月9日
 * @author： DongY
 * @version ：1.0
 * 	  1.[2018年12月9日]CreatBy：dy
 */

public class ConfigReader {

	private static byte[] insertArea;
	
	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
		Config config=read();
		System.out.println(config);
		if (null==config.getInputPath()) {
			System.err.println("文件不存在.........");
			return;
		}
		File file=new File(config.getInputPath());
		InputStream in=new FileInputStream(file);
		byte[] b=new byte[1024];
		while (in.read(b)>0) {
			if (isValid( b,config)) {
				continue;
			}
			if (1024>=in.available()) {
				b=new byte[in.available()];
			}else{
				b=new byte[1024];
			}
			
			if (in.available()<1024) {
				break;
			}
			
		}
		in.close();
	}
	
	/**
	 * 包处理
	* @Description: 
	* @param b	void
	 */
	public static void pcgHandler(byte[] b){
		byte[]bt=Arrays.copyOfRange(b, 10, b.length);
//		byte [] b0=Arrays.copyOf(bt, 2);
//		int type=
		int index=0;
		int length=0;
		do {
			length=getPckLength(bt);
			byte[]bts=Arrays.copyOfRange(bt, index, length);
			index+=7+length;
		} while (length+7<bt.length);
		
	}
	
	/**
	* @Description: 获取包长
	* @param bt
	* @return	int
	*/ 
	private static int getPckLength(byte[] bt) {
		byte[]bs=Arrays.copyOfRange(bt, 1, 3);
		return ByteUtils.toInt(bs, 1);
	}

	/**
	 * 
	* @Description: 判断插入域
	* @param bs
	* @return	boolean
	 */
	public static boolean isInster(byte[] bs){
		byte[]bts=Arrays.copyOf(bs, 2);
		int type=ByteUtils.toInt(bts, 5, 15);
		return type==1;
	}
	public static byte[] getInsertArea(byte[] bs,int length){
		bs=
		return bs;
	}

	/**
	* @Description: 根据帧头判断桢是否有效
	* @param b
	* @param config
	* @return	boolean
	 */
	public static boolean isValid(byte[] b,Config config){
		byte[] bt=Arrays.copyOf(b, 4);
		System.out.println(bt.length);
		int num=ByteUtils.toInt(bt);
		String strHex = Integer.toHexString(num);
		System.out.println(strHex.toUpperCase());
		return config.getVcHead().equals(strHex);
	}
	
//	public static 
	/**
	* @Description: 解析配置文件
	* @return
	* @throws IOException	Config
	 */
	public static Config read() throws IOException{
		String path=System.getProperty("user.dir");
		path=path+"/config.json";
		ObjectMapper mapper=new ObjectMapper();
		Config config=mapper.readValue(new File(path), Config.class);
		return config;
	}
	
}
